<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="服务名称" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.profile" placeholder="服务环境" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-input v-model="listQuery.content" placeholder="内容配置" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button  class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">
        添加配置
      </el-button>
    </div>
    <br/>

      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
      >
      <el-table-column align="left" label="序号ID" width="95">
        <template slot-scope="scope">
          {{scope.row.id}}
        </template>
      </el-table-column>
      <el-table-column label="服务名称" width="95">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="服务环境" width="95">
        <template slot-scope="scope">
          {{scope.row.profile}}
        </template>
      </el-table-column>
      <el-table-column label="最后更新人" width="95" align="left">
        <template slot-scope="scope">
          <span>{{ scope.row.editor }}</span>
        </template>
      </el-table-column>
      <el-table-column label="内容配置" align="left">
        <template slot-scope="scope">
              <div style="white-space:pre-line;height: 80px;overflow:auto;" v-html="scope.row.content">{{scope.row.content}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="最后更新时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.modify_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <router-link :to="'/configManager/list/'+row.id">
            <el-button type="primary" size="mini">
              历史版本
            </el-button>
          </router-link>
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <template>
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList" />
    </template>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;">
        <el-form-item label="服务名称" prop="name">
          <el-input v-if="dialogStatus==='create'" v-model="temp.name"  placeholder="Please input server name" />
          <el-input v-if="dialogStatus==='update'" disabled v-model="temp.name"  placeholder="Please input server name" />
        </el-form-item>
        <el-form-item label="服务环境" prop="profile">
          <el-input  v-if="dialogStatus==='create'"   v-model="temp.profile"  placeholder="Please input dev test prod fuse" />
          <el-input  v-if="dialogStatus==='update'"  disabled  v-model="temp.profile"  placeholder="Please input dev test prod fuse" />
        </el-form-item>
        <el-form-item label="内容配置" prop="content">
          <el-input v-model="temp.content" :autosize="{ minRows: 6, maxRows: 12}" type="textarea" placeholder="Please input server config" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getList, saveConfig, updateConfig, deleteConfigId } from '@/api/config'
  import Pagination from '@/components/Pagination'

  export default {
    components: { Pagination },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger'
        }
        return statusMap[status]
      }
    },
    data() {
      return {
        tableKey: 0,
        temp: {
          name: '',
          profile: '',
          content: ''
        },
        dialogStatus: '',
        dialogFormVisible: false,
        total: 0,
        list: null,
        listLoading: true,
        listQuery: {
          name: undefined,
          profile: undefined,
          content: undefined,
          page: 1,
          limit: 10
        },
        textMap: {
          update: '编辑配置中心',
          create: '添加配置中心'
        },
        rules: {
          name: [{ required: true, message: '请输入服务名称', trigger: 'change' }],
          profile: [{ required: true, message: '请输入服务环境', trigger: 'change' }],
          content: [{ required: true, message: '请输入内容配置', trigger: 'change' }]
        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      resetTemp() {
        this.temp = {
          name: '',
          profile: '',
          content: ''
        }
      },
      getList() {
        // 获取数据
        return this.fetchData()
      },
      handleCreate() {
        this.resetTemp()
        this.dialogStatus = 'create'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      createData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            saveConfig(this.temp).then(() => {
              this.dialogFormVisible = false
              this.$notify({
                title: 'Success',
                message: 'Created Successfully',
                type: 'success',
                duration: 2000
              })
              this.fetchData()
            })
          }
        })
      },
      fetchData() {
        this.listLoading = true
        getList({
          pageIndex: this.listQuery.page,
          pageSize: this.listQuery.limit,
          profile: this.listQuery.profile,
          content: this.listQuery.content,
          name: this.listQuery.name
        }).then(response => {
          this.list = response.result.data
          this.total = response.result.total
          this.listLoading = false
        })
      },
      handleUpdate(row) {
        this.temp =Object.assign({}, row) // copy obj
        if(this.temp.content!=undefined){
          let contentValue=this.temp.content
          let reg=new RegExp('<font color="red">','g')//g代表全部
          contentValue=contentValue.replace(reg,'');
          // console.log("======contentValue:",contentValue)
          reg=new RegExp('</font>','g')//g代表全部
          contentValue=contentValue.replace(reg,'');
          // console.log("======contentValue:",contentValue)
          this.temp.content=contentValue
        }
        this.dialogStatus = 'update'
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      },
      updateData() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const tempData = Object.assign({}, this.temp)
            updateConfig(tempData).then(() => {
              this.dialogFormVisible = false
              this.$notify({
                title: 'Success',
                message: 'Update Successfully',
                type: 'success',
                duration: 2000
              })
              this.fetchData()
            })
          }
        })
      },
      handleFilter() {
        this.listQuery.page = 1
        this.getList()
      },
      handleDelete(row) {
        if (!confirm("你确定要删除吗")){
          return
        }
        this.temp = Object.assign({}, row) // copy obj
        deleteConfigId(this.temp).then(() => {
          this.dialogFormVisible = false
          this.$notify({
            title: 'Success',
            message: 'Delete Successfully',
            type: 'success',
            duration: 2000
          })
          this.fetchData()
        })
      }
    }
  }
</script>
